function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
var lessonidx = getUrlVars()['id'];
          
$(function() {
    $(".button").click(function() {
        // validate and process form here
        var message = $("input#message").val();  
        var lessonid = $("input#lessonid").val();  
        var chattype = $("input[name='chattype']:checked").val();
        var dataString = 'chattype='+ chattype  + '&message='+ message + '&lessonid=' + lessonid;
        $.ajax({
            type: "POST",
            url: "ChatMessage",
            data: dataString,
            success: function() {
                document.chatform.message.value = "";
                $("input#message").focus();
            }
        });
        return false;
    });
});
  
function refreshData() {
    $('#chatwindow').load('ChatReceive?lessonid='+lessonidx, function() {
        var chat = document.getElementById("chatwindow");
        if (chat.scrollTop + 250 > chat.scrollHeight - 50) { // 250 height of window
            chat.scrollTop = chat.scrollHeight;
        }
    });
}
// Execute every 1 second

function refreshQuestionData() {
    $('#questionwindow').load('QuestionReceive?lessonid='+lessonidx, function() {
        var question = document.getElementById("questionwindow");
        if (question.scrollTop + 250 > question.scrollHeight - 50) { // 250 height of window
            question.scrollTop = question.scrollHeight;
        }
    });
}

window.setInterval(refreshData, 500);
window.setInterval(refreshQuestionData, 500);